<template>  
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span v-for="item,index in options1" :key="index" :class="{active:options1curr == index}" @click="options1Fn(index,item.min,item.max)">{{ item.txt }}</span>
        </li>
        <li>
          <strong>分类：</strong>
          <span v-for="item,index in options2" :key="index" :class="{active:options2curr == index}"  @click="options2Fn(index,item.type)">{{ item.txt }}</span>
        </li>
      </ul>
      <List :arr="goodsListShow" />
      <div>{{isReachBottom ? '加载完毕!' : '加载中....'}}</div>
    </div>
  </div>
</template>                            
<script>

import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { GoodsSearchAPI } from "@/request/api";
import { getScrollTop,getClientHeight,getScrollHeight } from "@/utils/index"
export default {
  data() {
    return {
      //  产品列表
      goodsList:[],
      goodsListShow:[],
      page:1,
      size:8,
      isLoading:false,
      isReachBottom : false,
      did:0,
      type:0,
      min:0,
      max:0,
      keyword:"",
      options1:[
        { min: 0, max: 0, txt: "全部" },
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      options2: [
        { type: 0, txt: "全部" },
        { type: 1, txt: "实体商品" },
        { type: 2, txt: "虚拟商品" },
      ],
      options1curr:0,
      options2curr:0,
    };
  },
  components: {
    Crumb,
    List,
  },
  created() {
    
    let keyword = this.$route.query.keyword;
    let did = +[this.$route.query.did ? this.$route.query.did : 0]
    // console.log(keyword);
    // console.log(did);
    this.keyword = keyword
    this.did = did
    this.getGoods()
  },
  mounted(){
    window.addEventListener("scroll",this.scrollFn); 
  },
  beforeDestroy() {
    window.removeEventListener("scroll",this.scrollFn);
  },
  watch: {
    "$route.query.keyword":{
      handler(newVal,oldVal) {
        // console.log(newVal,oldVal);
        this.keyword = newVal;
        this.getGoods();
      }
    }
  },
  methods:{
    async getGoods(){
      let res = await GoodsSearchAPI({
      did:this.did,
      type:this.type,
      min:this.min,
      max:this.max,
      keyword:this.keyword
    });
    // console.log(res.data);
    this.goodsList = res.data;
    this.goodsListShow = this.goodsList.filter((item, index) => index < 8);
    
    this.page=1;
    console.log(this.page);
    this.isReachBottom = false;
    if(this.goodsListShow.length == this.goodsList.length){
          this.isReachBottom = true;
        }
    },
    options1Fn(idx,min,max){
      this.options1curr = idx;
      this.min = min;
      this.max = max;
      // 重新请求数据
      this.getGoods();
    },
    options2Fn(idx,type){
      this.options2curr = idx;
      this.type = type;
      // 重新请求数据
      this.getGoods();
    },
    scrollFn() {
      console.log("滚动了>>>>",getScrollTop(),getClientHeight(),getScrollHeight());
      if(getScrollTop()+getClientHeight() >= getScrollHeight() - 240){
        if(this.goodsListShow.length == this.goodsList.length){
          this.isReachBottom = true;
          return;
        }
        console.log("准备加载下一页");
        if(this.isLoading == false) {
          this.isLoading = true;
          this.page++;
          setTimeout(()=>{
            for(let i=this.size*(this.page - 1);i<this.size*this.page;i++){
            if(i>this.goodsList.length-1){
              return;
            }
            console.log("准备开始push数据",this.page,i);
            this.goodsList[i] &&this.goodsListShow.push(this.goodsList[i]);
          }
          this.isLoading = false;
          },1000);
        }
        
        
      }
    }
  }
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right:20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>